package gui;

import database.*;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class PanelNyRett extends JPanel implements ActionListener{
	private JLabel labelNavn,labelBeskrivelse,labelPris, labelNyRett;
	private JTextField fieldNavn,fieldBeskrivelse,fieldPris;
	private JButton buttonOK, buttonTilbake, buttonHovedmeny;
	private GridBagConstraints constraints;
	public PanelNyRett(){
		this.setLayout(new GridBagLayout());
		this.leggTilKomponenter();
		GUI.getVindu().add(this);
	}
	private void leggTilKomponenter(){
		labelNavn = new JLabel("Navn");
		labelBeskrivelse = new JLabel("Beskrivelse");
		labelPris = new JLabel("Pris");
		fieldNavn = new JTextField(20);
		fieldBeskrivelse = new JTextField(20);
		fieldPris = new JTextField(20);
		
		labelNyRett = new JLabel("Legg til ny rett");
		Font f = new Font(labelNyRett.getFont().getName(), labelNyRett.getFont().getStyle(), 30);
		labelNyRett.setFont(f);
		
		buttonOK = new JButton("OK");
		buttonOK.addActionListener(this);
		
		buttonTilbake = new JButton("Tilbake");
		buttonTilbake.addActionListener(this);
		
		buttonHovedmeny = new JButton("Tilbake til hovedmeny");
		buttonHovedmeny.addActionListener(this);
		
		constraints = new GridBagConstraints();
		constraints.insets = new Insets(10,10,10,10);
		constraints.gridy = 0;
		constraints.gridwidth = 2;
		this.add(labelNyRett,constraints);
		constraints.gridwidth = 1;
		constraints.gridy = 1;
		constraints.gridx = 0;
		this.add(labelNavn,constraints);
		constraints.gridx = 1;
		this.add(fieldNavn,constraints);
		constraints.gridy = 2;
		constraints.gridx = 0;
		this.add(labelBeskrivelse,constraints);
		constraints.gridx = 1;
		this.add(fieldBeskrivelse,constraints);
		constraints.gridy = 4;
		constraints.gridx = 0;
		this.add(labelPris,constraints);
		constraints.gridx = 1;
		this.add(fieldPris,constraints);
		
		constraints.gridy = 5;
		constraints.gridx = 0;
		this.add(buttonOK,constraints);
		constraints.gridx = 1;
		this.add(buttonTilbake,constraints);
		constraints.gridx = 0;
		constraints.gridy = 6;
		constraints.gridwidth = 2;
		this.add(buttonHovedmeny, constraints);
		
		
	}
	private void buttonOK_actionPerformed(ActionEvent e)
	{
		if(valider()){
			this.setVisible(false);
			Databasecon.addRett(fieldNavn.getText(), fieldBeskrivelse.getText(), Integer.parseInt(fieldPris.getText()),"ja");
			new PanelAlleRetter();			
		}
	}
	public boolean valider(){
		if(!Register.validerTallOgBokstaverTillat(fieldNavn.getText())|| fieldNavn.getText().isEmpty()){
			JOptionPane.showMessageDialog(null, "Ugyldig navn");
			return false;
			
		}
		else if(!Register.validerTallOgBokstaverTillat(fieldBeskrivelse.getText())|| fieldBeskrivelse.getText().isEmpty()){
			JOptionPane.showMessageDialog(null, "Ugyldig beskrivelse");
			return false;
		}
		else if(!Register.validerPris(fieldPris.getText())||fieldPris.getText().isEmpty()){
			JOptionPane.showMessageDialog(null, "Ugyldig pris");
			return false;
		}
		else{
			return true;
		}
	}
	private void buttonTilbake_actionPerformed(ActionEvent e)
	{
		this.setVisible(false);
		new PanelMeny();
	}
	private void buttonHovedmeny_actionPerformed(ActionEvent e)
	{
		this.setVisible(false);
		new PanelHovedmeny();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==buttonOK)
			buttonOK_actionPerformed(e);
		else if(e.getSource()==buttonTilbake)
			buttonTilbake_actionPerformed(e);
		else if(e.getSource()==buttonHovedmeny)
			buttonHovedmeny_actionPerformed(e);
		
	}
}
